﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System.Collections.Generic;
using System.Linq;

namespace Pipra.Uom.Core
{
    public class CoreScaleAuthority :
        IUomAuthority
    {
        private static readonly double[] ValuesPp = new double[]{
            100,
            1000,
            10000,
            1000000,
            1000000000,
            1000000000000,
            1000000000000000
        };

        private static readonly ConversionFactorEntry[] ValuesPpEntries = ValuesPp
            .Select(value =>new ConversionFactorEntry(value))
            .ToArray();

        private static readonly ConversionFactorEntry[] ValuesPpEntriesInverted = ValuesPp
            .Select(value => new ConversionFactorEntry(1, value))
            .ToArray();

        private readonly Uom[] AllScaleUnits;

        private readonly Uom UomUnity;
        private readonly Uom UomUno;

        private readonly UomComparer _cmp;

        public CoreScaleAuthority() : this(null) { }

        public CoreScaleAuthority(UomComparer cmp)
        {
            _cmp = cmp ?? new UomComparer();
            AllScaleUnits = NameConstants.AllScaleNames
                .Select(n => new Uom(n,this))
                .ToArray()
            ;
            UomUnity = new Uom(NameConstants.NameUnity,this);
            UomUno = new Uom(NameConstants.NameUno,this);
        }

        private int FindValuesPpIndex(IUom uom)
        {
            return null != uom ? FindValuesPpIndex(uom.Name) : -1;
        }

        private int FindValuesPpIndex(string name)
        {
            if (name.Length > NameConstants.NameScalePrefixPpSpace.Length)
            {
                _cmp.Equals(NameConstants.NameScalePrefixPpSpace, name.Substring(0, NameConstants.NameScalePrefixPpSpace.Length));
                string suffix = name.Substring(NameConstants.NameScalePrefixPpSpace.Length);
                for (int i = 0; i < NameConstants.NameQuantitySuffix.Length; i++)
                {
                    if (_cmp.Equals(suffix, NameConstants.NameQuantitySuffix[i]))
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        public string Type
        {
            get { return NameConstants.NameScale; }
        }


        public IEnumerable<IUom> Units
        {
            get { return AllScaleUnits.AsEnumerable().Cast<IUom>() ; }
        }

        public IUom GetUnit(string name)
        {
            return Units.FirstOrDefault(u => _cmp.Equals(u.Name,name));
        }

        public UomComparer Comparer
        {
            get { return _cmp; }
        }


        public IEnumerable<IUom> GetMappedUnits(IUom from)
        {
            if (AllScaleUnits.Contains(from, _cmp))
            {
                return AllScaleUnits.Where(uom => !_cmp.Equals(from, uom)).Cast<IUom>();
            }
            return Enumerable.Empty<IUom>();
        }

        private ConversionFactorEntry GetEntry(IUom from, IUom to)
        {
            bool fromUnity = _cmp.Equals(from, UomUnity) || _cmp.Equals(from, UomUno);
            bool toUnity = _cmp.Equals(to, UomUnity) || _cmp.Equals(to, UomUno);

            if (fromUnity)
            {
                if (toUnity)
                {
                    return ConversionFactorEntry.Unity;
                }
                int toPpIndex = FindValuesPpIndex(to);
                if (toPpIndex >= 0)
                {
                    return ValuesPpEntries[toPpIndex];
                }
            }
            else
            {
                int fromPpIndex = FindValuesPpIndex(from);
                if (toUnity)
                {
                    if (fromPpIndex >= 0)
                    {
                        return ValuesPpEntriesInverted[fromPpIndex];
                    }
                }
                else
                {
                    int toPpIndex = FindValuesPpIndex(to);
                    if (fromPpIndex >= 0 && toPpIndex >= 0)
                    {
                        return new ConversionFactorEntry(
                            ValuesPp[fromPpIndex],
                            ValuesPp[toPpIndex]
                        );
                    }
                }
            }
            return null;
        }

        public IValueConverter GetConverter(IUom from, IUom to)
        {
            return FactorValueConverter.CreateConverter(Type, from, to, GetEntry(from, to));
        }

    }
}
